package com.ulic.customerManApplication.memberenjoymodule.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;

import com.ulic.customerManApplication.base.BaseDao;
import com.ulic.customerManApplication.constant.CONSTANT;
import com.ulic.customerManApplication.memberenjoymodule.bo.MemberEnjoyActivity;
import com.ulic.customerManApplication.memberenjoymodule.dao.MemberEnjoyDao;
import com.ulic.customerManApplication.memberenjoymodule.vo.MemberEnjoyVO;
import com.ulic.customerManApplication.pager.PagerModel;
@Component("memberEnjoyDao")
public class MemberEnjoyDaoImpl extends BaseDao implements MemberEnjoyDao {

	Session session = null;
	@Override
	public void insertMemberEnjoyActivities(
			List<MemberEnjoyActivity> memberEnjoyActivities) throws Exception {
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		for(MemberEnjoyActivity memberEnjoyActivity:memberEnjoyActivities){
			session.save(memberEnjoyActivity);
		}
		session.flush();
	}

	@Override
	public void insertMemberEnjoyActivity(MemberEnjoyActivity memberEnjoyActivity)
			throws Exception {
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		try{
			session.save(memberEnjoyActivity);
		}catch (Exception e) {
			e.printStackTrace();
		}
		session.flush();
	}


	@Override
	public MemberEnjoyActivity getMemberEnjoyActivity(int id) {
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Criteria cri = session.createCriteria(MemberEnjoyActivity.class);
		List list = cri.add(Restrictions.eq("id", id)).list();
		MemberEnjoyActivity memberEnjoyActivity = null;
		if(list.size()>0){
			memberEnjoyActivity	= (MemberEnjoyActivity)list.get(0);
		}
		return memberEnjoyActivity;
	}

	@Override
	public void updateMemberEnjoyActivity(
			MemberEnjoyActivity memberEnjoyActivity) throws Exception {
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		session.saveOrUpdate(memberEnjoyActivity);
		session.flush();
	}

	@Override
	public void deleteMemberEnjoyActivity(
			MemberEnjoyActivity memberEnjoyActivity) throws Exception {
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		session.delete(memberEnjoyActivity);
		session.flush();
	}

	@Override
	public PagerModel getMemberEnjoyActivities(int curPage) {
		String hql = "from MemberEnjoyActivity memberEnjoyActivity order by createDate desc";
		return searchPaginated(hql, curPage, CONSTANT.PageSize);
	}

	@Override
	public PagerModel getMemberEnjoyActivities(MemberEnjoyVO memberEnjoyVO,
			int curPage) {
		StringBuilder sb = new StringBuilder();
		sb.append("from MemberEnjoyActivity memberEnjoy where 1=1 ");
		if(memberEnjoyVO != null ){
			if(memberEnjoyVO.getStartDate() != null && memberEnjoyVO.getStartDate().length() != 0){
				sb.append(" and to_date(memberEnjoy.activityDate,'YYYY-MM-DD  HH24:MI:SS') >= to_date('"+memberEnjoyVO.getStartDate()+" 00:00:00"+"','YYYY-MM-DD  HH24:MI:SS') ");
			}
			if(memberEnjoyVO.getEndDate() != null && memberEnjoyVO.getEndDate().length() != 0){
				sb.append(" and to_date(memberEnjoy.activityDate,'YYYY-MM-DD  HH24:MI:SS') <= to_date('"+memberEnjoyVO.getEndDate()+" 23:59:59"+"','YYYY-MM-DD  HH24:MI:SS') ");
			}
			if(memberEnjoyVO.getMemberEnjoy() != null){
				MemberEnjoyActivity memberEnjoy = memberEnjoyVO.getMemberEnjoy();
				if(memberEnjoy.getEnjoyActivity() != null && memberEnjoy.getEnjoyActivity().length() !=0){
					sb.append(" and memberEnjoy.enjoyActivity like '%"+memberEnjoy.getEnjoyActivity().trim()+"%'");
				}
				if(memberEnjoy.getSubCompany()!=null&&!"No".equals(memberEnjoy.getSubCompany())){
					sb.append(" and memberEnjoy.subCompany = "+memberEnjoy.getSubCompany());
				}
				if(memberEnjoy.getSubCity()!=null&&!"No".equals(memberEnjoy.getSubCity())){
					sb.append(" and memberEnjoy.subCity = "+memberEnjoy.getSubCity());
				}
				if(memberEnjoy.getOaSph() != null && !"No".equals(memberEnjoy.getOaSph().trim()) && !"".equals(memberEnjoy.getOaSph().trim())){
					sb.append(" and memberEnjoy.oaSph like '%"+memberEnjoy.getOaSph().trim()+"%'");
				}
			}
		}
		sb.append(" order by activityDate desc");
		return searchPaginated(sb.toString(), curPage, CONSTANT.PageSize);
	}

	@Override
	public PagerModel getMemberEnjoyActivities(MemberEnjoyVO memberEnjoyVO,
			int curPage, int totalNum) {
		StringBuilder sb = new StringBuilder();
		sb.append("from MemberEnjoyActivity memberEnjoy where 1=1 ");
		if(memberEnjoyVO != null ){
			if(memberEnjoyVO.getStartDate() != null && memberEnjoyVO.getStartDate().length() != 0){
				sb.append(" and to_date(memberEnjoy.activityDate,'YYYY-MM-DD  HH24:MI:SS') >= to_date('"+memberEnjoyVO.getStartDate()+" 00:00:00"+"','YYYY-MM-DD  HH24:MI:SS') ");
			}
			if(memberEnjoyVO.getEndDate() != null && memberEnjoyVO.getEndDate().length() != 0){
				sb.append(" and to_date(memberEnjoy.activityDate,'YYYY-MM-DD  HH24:MI:SS') <= to_date('"+memberEnjoyVO.getEndDate()+" 23:59:59"+"','YYYY-MM-DD  HH24:MI:SS') ");
			}
			if(memberEnjoyVO.getMemberEnjoy() != null){
				MemberEnjoyActivity memberEnjoy = memberEnjoyVO.getMemberEnjoy();
				if(memberEnjoy.getEnjoyActivity() != null && memberEnjoy.getEnjoyActivity().length() !=0){
					sb.append(" and memberEnjoy.enjoyActivity like '%"+memberEnjoy.getEnjoyActivity().trim()+"%'");
				}
				if(memberEnjoy.getSubCompany()!=null&&!"No".equals(memberEnjoy.getSubCompany())){
					sb.append(" and memberEnjoy.subCompany = "+memberEnjoy.getSubCompany());
				}
				if(memberEnjoy.getSubCity()!=null&&!"No".equals(memberEnjoy.getSubCity())){
					sb.append(" and memberEnjoy.subCity = "+memberEnjoy.getSubCity());
				}
				if(memberEnjoy.getOaSph() != null && !"No".equals(memberEnjoy.getOaSph().trim()) && !"".equals(memberEnjoy.getOaSph().trim())){
					sb.append(" and memberEnjoy.oaSph like '%"+memberEnjoy.getOaSph().trim()+"%'");
				}
			}
		}
		sb.append(" order by activityDate desc");
		return searchPaginated(sb.toString(), curPage, totalNum);
	}

}
